using System;
using System.Diagnostics;
using System.Threading;

namespace BassMati.Diagnostics 
{
	public class TestThread
	{
		public TestThread ( string threadName, int interval )
		{
			this.thread = new System.Threading.Thread ( new ThreadStart( this.Work ));
			this.thread.Name = threadName;
			this.thread.IsBackground = true;
			this.interval = interval;
			this.thread.Start( );
		}

		protected Thread thread;
		protected int interval;
		protected bool abortRequest;

		public void RequestTermination ( )
		{
			this.abortRequest = true;
		}

		protected void Work ( )
		{
			try
			{
				Trace.WriteLine( "Work started.", "Info" );
				while ( true )
				{
					System.Threading.Thread.Sleep( this.interval );
					if ( this.abortRequest ) break;
					Trace.WriteLine( "Work method", "Info" );
					this.TestMethod( );
				}
			}
			catch ( Exception ex )
			{
				Trace.WriteLine( ex.Message, "Error" );
			}
			finally
			{
				Trace.WriteLine( "Work exit.", "Info" );
			}
		}

		protected void TestMethod( )
		{
			Trace.WriteLine( "TestMethod", "Enter" );
			System.Diagnostics.StackTrace stacktrace = new StackTrace( );
			Trace.WriteLine( string.Format( "Frame count = {0}", stacktrace.FrameCount ), "Info" );
			Trace.WriteLine( "TestMethod", "Exit" );
		}
	}
}
